package com.hls.zfbai.modules.business.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hls.zfbai.core.utils.CommonConstants;
import com.hls.zfbai.modules.business.entity.GhjlParam;
import com.hls.zfbai.modules.business.entity.MsGhmxZfbAi;
import com.hls.zfbai.modules.common.dao.CommonMapper;
import com.hls.zfbai.modules.common.entity.GyXtcs;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.annotation.Order;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

@Slf4j
@Service
public class ZfbAiGhjlService {
    @Resource
    private MsGhmxZfbAiService msGhmxZfbAiService;
    @Resource
    private ZfbAiService zfbAiService;
    @Resource
    private CommonMapper commonMapper;

    @Scheduled(fixedRate = 1000)
    public void run() {
        GyXtcs qycsCs = new GyXtcs("1", "ZFBAI_SERVICE_SFQY", "0", "支付宝AI云陪诊接口是否启用");
        commonMapper.getXtcs(qycsCs);
        String qycs = qycsCs.getCSZ();
        GyXtcs qyghCs = new GyXtcs("1", "ZFBAI_SERVICE_SFQY_GHJL", "0", "支付宝AI云陪诊接口是否启用");
        commonMapper.getXtcs(qyghCs);
        String qygh = qyghCs.getCSZ();
        if(CommonConstants.YES.equals(qycs) && CommonConstants.YES.equals(qygh)) {
            LocalDateTime localDateTime = LocalDateTime.now();
            QueryWrapper<MsGhmxZfbAi> queryWrapper = new QueryWrapper<>();
            queryWrapper.inSql("JLXH","SELECT JLXH FROM MS_GHMX_ZFBAI WHERE JLSJ > SYSDATE - 5/1440 AND TSBZ = 0");
            List<MsGhmxZfbAi> msGhmxZfbAiList = msGhmxZfbAiService.list(queryWrapper);

            log.info("获取未推送的挂号信息，并进行推送定时任务开始执行！");

            if(!CollectionUtils.isEmpty(msGhmxZfbAiList)) {
                log.info("获取未推送的挂号信息，共获取到信息数：{}条", msGhmxZfbAiList.size());
                DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
                String now = dateTimeFormatter.format(localDateTime);
                for(MsGhmxZfbAi msGhmxZfbAi : msGhmxZfbAiList) {
                    GhjlParam ghjlParam = new GhjlParam();
                    BeanUtils.copyProperties(msGhmxZfbAi, ghjlParam);
                    ghjlParam.setTradeCode("1001");
                    ghjlParam.setDatasetCode("HDSD.GHJL");
                    ghjlParam.setMedHosCode(msGhmxZfbAi.getYqsb().toString());
                    ghjlParam.setScbs("0");
                    ghjlParam.setTxnr("请前往诊区签到后排队就诊");
                    ghjlParam.setYndhcs("{\"ynksbm\":\"" + msGhmxZfbAi.getJzksbzdm() + "\",\"ynksmc\":\"" + msGhmxZfbAi.getJzksyynbmc() + "\",\"ynwzmc\":\"" + msGhmxZfbAi.getJzkswz() + "\"}");
                    ghjlParam.setSjgxsj(now);
                    //调用支付宝接口
                    zfbAiService.ghjl(ghjlParam);
                    //更新推送状态
                    msGhmxZfbAi.setTsbz(1);
                    msGhmxZfbAi.setTssj(localDateTime);
                    msGhmxZfbAiService.updateById(msGhmxZfbAi);
                }
            } else {
                log.info("获取未推送的挂号信息，共获取到信息数：0条");
            }
        } else {
            log.info("获取未推送的挂号信息，并进行推送定时任务未执行！原因：系统参数ZFBAI_SERVICE_SFQY未启用");
        }
    }
}